package jobflow.dao;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import jobflow.entity.Departman;
import org.apache.commons.lang3.StringUtils;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Synchronized;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.faces.FacesMessages;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;

@Name("departmanDao2")
@Scope(ScopeType.CONVERSATION)
@AutoCreate
@Synchronized
public class DepartmanDao2 implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2386902108899852909L;

	@In
	EntityManager entityManager;

	@In
	FacesMessages facesMessages;

	private String encodedImage = null;

	private Departman departman = new Departman();

	private List<Departman> departmanList = new ArrayList<Departman>();

	private Departman currentRow;
	
	@In DepartmanDao2 departmanDao2;

	private Departman newDepartman = new Departman();

	@Create
	@Begin
	public void begin() throws IOException{
     
		departmanDao2.departmanAra();	
	}

	public void listener(UploadEvent event) throws Exception {
		UploadItem item = event.getUploadItem();
		departman.setData(item.getData());
	}

	public void listener2(UploadEvent event) throws Exception {

		UploadItem item = event.getUploadItem();

		newDepartman.setData(item.getData());
	}

	public void departmanRowAdd() {

		Departman departman = new Departman();

		newDepartman = departman;

		newDepartman.setVisible(true);

		departmanList.add(0, newDepartman);

	}

	public void departmanSave(Departman departman) {

		if (StringUtils.isEmpty(departman.getDepartmanAdi())) {

			facesMessages.add("departman Adi giriniz");

			return;

		} else if (StringUtils.isEmpty(departman.getDepartmanSorumlusu())) {

			facesMessages.add("departman Sorumlusu giriniz");

			return;

		} else if (StringUtils.isEmpty(departman.getDahiliTel())) {
			facesMessages.add("Dahili Tel giriniz");

			return;

		} else {

			entityManager.persist(departman);

			departman.setVisible(false);

			departman = new Departman();

			facesMessages.addFromResourceBundle("departman.eklenmisitir");
		}

	}

	public void departmanSil() {

		entityManager.remove(currentRow);

		departmanList.remove(currentRow);

		File file = new File(
				"E:/worspaceler/seam_calisma/seam_calisma/jobflow/WebContent"
						+ currentRow.getImage());
		if (file.exists()) {
			file.delete();
		}

		facesMessages.addFromResourceBundle("departman.sil");
	}

	public void departmanCancel(Departman departman) {

		departmanList.remove(departman);

	}

	

	public void temizle() {

		departman = new Departman();

		departmanList.clear();

		entityManager.clear();

	}

	public void pasif(Departman departman) {

		entityManager.createQuery("update Departman d set d.aktif=0")
				.executeUpdate();

		entityManager.refresh(departman);

	}

	public void aktif(Departman departman) {
		entityManager.createQuery("update Departman d set d.aktif=1")
				.executeUpdate();
		entityManager.refresh(departman);

	}

	public void departmanGuncel(Departman departman) {

		Departman depart = entityManager.find(Departman.class,
				departman.getId());

		this.departman = depart;

	}
	

	public void departmanAra() throws IOException {

		entityManager.clear();

		String strSQL = "from Departman d where 1=1";

		if (!StringUtils.isEmpty(departman.getDepartmanAdi())) {
			strSQL += " and d.departmanAdi=:departmanAdi";
		}
		if (!StringUtils.isEmpty(departman.getDepartmanSorumlusu())) {
			strSQL += " and d.departmanSorumlusu=:departmanSorumlusu";
		}
		if (!StringUtils.isEmpty(departman.getDahiliTel())) {
			strSQL += " and d.dahiliTel=:dahiliTel";
		}
		if (departman.getAktif() != null) {
			strSQL += " and d.aktif=:aktif";
		}

		Query query = entityManager.createQuery(strSQL);

		if (!StringUtils.isEmpty(departman.getDepartmanAdi())) {
			query.setParameter("departmanAdi", departman.getDepartmanAdi());
		}
		if (!StringUtils.isEmpty(departman.getDepartmanSorumlusu())) {
			query.setParameter("departmanSorumlusu",
					departman.getDepartmanSorumlusu());
		}
		if (!StringUtils.isEmpty(departman.getDahiliTel())) {
			query.setParameter("dahiliTel", departman.getDahiliTel());
		}
		if (departman.getAktif() != null) {
			query.setParameter("aktif", departman.getAktif());

		}

		departmanList = query.getResultList();

		int i = 0;

		for (Departman departmanResim : departmanList) {

			i++;
			
		if (departmanResim.getData() != null) {
			String path = "E:/worspaceler/seam_calisma/seam_calisma/jobflow"
					+ "/WebContent/images/departman/" + i + ".jpeg";

			File of = new File(path);

			FileOutputStream osfd = new FileOutputStream(of);

		
				osfd.write(departmanResim.getData());
				osfd.flush();
				osfd.close();

				path = "/images/departman/" + i + ".jpeg";

				departmanResim.setImage(path);
			}

		}

	}
	
	public String exportExcel() throws Exception {
		
		
		Contexts.getConversationContext().set("excelExported", false);
		departmanDao2.departmanAra();
		
		return "/departman/excelExport.xhtml";

		
	}

	public String geri() {

		departmanList.clear();
		return "/departmanAra.xhtml";

	}

	public void setDepartman(Departman departman) {
		this.departman = departman;
	}

	public Departman getDepartman() {
		return departman;
	}

	public void setDepartmanList(List<Departman> departmanList) {
		this.departmanList = departmanList;
	}

	public List<Departman> getDepartmanList() {
		return departmanList;
	}

	public void setCurrentRow(Departman currentRow) {
		this.currentRow = currentRow;
	}

	public Departman getCurrentRow() {
		return currentRow;
	}

	public void setEncodedImage(String encodedImage) {
		this.encodedImage = encodedImage;
	}

	public String getEncodedImage() {
		return encodedImage;
	}

	public void setNewDepartman(Departman newDepartman) {
		this.newDepartman = newDepartman;
	}

	public Departman getNewDepartman() {
		return newDepartman;
	}
   

}
